<?
include '../includes/specs_functions.php';

#----------------------------------------------------
# Connect to PADS
$DB = getDB();

$jobId = $_POST['jobId'];
$skillName = $_POST['skillName'];
$level = $_POST['level']; // Max level ignore all other skills
$isSearch = false;
if ($skillName != -1)
{
	$isSearch = true;
}

$col = array(
		"ab.".TBL_ABILITIES_ID." as id"
		,"ab.".TBL_ABILITIES_ENNAME." as name"
		,"ab.".TBL_ABILITIES_LEVEL." as level"
		,"ab.".TBL_ABILITIES_MP." as mp"
		,"ab.".TBL_ABILITIES_HP." as hp"
		,"ab.".TBL_ABILITIES_TP." as tp"
		,"ab.".TBL_ABILITIES_CASTTIME." as cast"
		,"ab.".TBL_ABILITIES_RECASTTIME." as recast"
		,"ab.".TBL_ABILITIES_RANGE." as range"
		,"ab.".TBL_ABILITIES_ICON." as icon"
		,"ab.".TBL_ABILITIES_ENDESC." as description"
		);

$table = TBL_ABILITIES." as ab";

$allowedSkills = getClassArray();
unset($allowedSkills["0"]);

$where = array(
			"ab.".TBL_ABILITIES_ID." NOT IN (".getCensoredSkills().")"
			,"ab.".TBL_ABILITIES_CLASS." IN (".implode(",",array_keys($allowedSkills)).")"
			);

if($isSearch)
{
		$table = "(".TBL_ABILITIES." as ab
					LEFT JOIN 
					".SPEC_TBL_JOBS." as job
					ON job.".SPEC_TBL_JOBS_ID." = ab.".TBL_ABILITIES_CLASS.")";
					
		array_push($col, "job.".SPEC_TBL_JOBS_NAME." as class");
		
		array_push($where, "ab.".TBL_ABILITIES_ENNAME." like '".$skillName."%'");
}


if($jobId != -1)
{
	array_push($where, "ab.".TBL_ABILITIES_CLASS." = \"".$jobId."\"");
}
if($level != -1)
{
	array_push($where, "ab.".TBL_ABILITIES_LEVEL." <= \"".$level."\"");
}
			

$orderBy = "ab.".TBL_ABILITIES_LEVEL;
$direction = "ASC";

$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $direction);

//Show($result);

$jobs = array();

foreach ($result[3] as $row) {
		
	$job = array( 
					"id" => $row['id']
					,"name" => $row['name']
					,"level" => $row['level']
					,"mp" => $row['mp']
					,"hp" => $row['hp']
					,"tp" => $row['tp']
					,"cast" => $row['cast']
					,"recast" => $row['recast']
					,"range" => $row['range']
					,"icon" => $row['icon']
					,"desc" => $row['description']
					,"type" => "skill"
					);
	if($isSearch){
		$job["job"] = $row['class'];
		$job["search"] = $isSearch;
	}
	array_push($jobs, $job);
}
	
echo json_encode($jobs);
	
#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);


?>